package com.briup.estore.adminAPI.web.controller;

import com.briup.estore.common.util.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author adam
 * @date 2022/10/19
 */
@RestController
@RequestMapping("file")
@Api(tags = "文件管理")
@CrossOrigin
public class FileController {
    @Value("${nginx.file-path}")
    private String fileDir;


    @PostMapping("/upload")
    @ApiOperation("文件上传至图片服务器")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", allowableValues = "goods,users,banners", paramType = "query", value = "根据上传的图片所属业务选择对应的query参数")
    })
    public Result uploadImage(String query, @RequestParam MultipartFile multipartFile) throws IOException {

        String filePath = fileDir + "/" + query + "/";
        String name = UUID.randomUUID().toString() + "." + multipartFile.getOriginalFilename().split("[.]")[1];
        File file = new File(filePath, name);
        multipartFile.transferTo(file);
        return Result.success(file.getName());

    }


}
